package com.xy.tianyu.module.picture.fragment

import android.view.View
import androidx.lifecycle.rxLifeScope
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.xy.tianyu.LazyLoadBaseFragment
import com.xy.tianyu.R
import com.xy.tianyu.module.picture.activity.PicSearchActivity
import com.xy.tianyu.module.picture.adapter.PicCategoryMoreOneAdapter
import com.xy.tianyu.module.picture.adapter.PicCategoryMoreTwoAdapter
import com.xy.tianyu.module.picture.bean.PicMoreCategoryBean
import com.xy.tianyu.module.picture.bean.S
import com.xy.tianyu.network.Api
import kotlinx.android.synthetic.main.fragment_pic_category_child.*
import rxhttp.RxHttp
import rxhttp.toClass

/**
 * @time 2020/10/13 11:05
 * @class 描述:
 */
class PicCategoryChildFragment:LazyLoadBaseFragment() {
    private lateinit var s: S

    override fun setContentView(): Int {
        return R.layout.fragment_pic_category_child
    }

    override fun lazyLoad() {
        s = arguments?.get("category") as S
        request()
    }

    override fun stopLoad() {

    }

    private fun request(){
        rxLifeScope.launch({
            val picMoreCategoryBean = getMoreCategory()
            val list = picMoreCategoryBean.s
            if (!list.isNullOrEmpty()){
                //取出集合中的第一个元素判断这个元素中的s是否为空
                val childList = list[0].s
                if (childList.isNullOrEmpty()){
                    //没有子元素
                    ry_pic_cate_more.layoutManager = GridLayoutManager(activity,2)
                    val picCategoryMoreOneAdapter = PicCategoryMoreOneAdapter(R.layout.item_pic_cate_one,list)
                    ry_pic_cate_more.adapter = picCategoryMoreOneAdapter

                    picCategoryMoreOneAdapter.setOnItemClickListener(object :OnItemClickListener{
                        override fun onItemClick(
                            adapter: BaseQuickAdapter<*, *>,
                            view: View,
                            position: Int
                        ) {
                            PicSearchActivity.actionStart(activity!!,list[position].t)
                        }

                    })
                }else{
                    //含有子元素
                    ry_pic_cate_more.layoutManager = LinearLayoutManager(activity)
                    val picCategoryMoreTwoAdapter = PicCategoryMoreTwoAdapter(R.layout.item_pic_cate_two,list)
                    ry_pic_cate_more.adapter = picCategoryMoreTwoAdapter
                }
            }
        },{

        },{

        },{

        })
    }
    private suspend fun getMoreCategory(): PicMoreCategoryBean {
        return RxHttp.get(Api.DAY_DAY_SEARCH_PICTURE_CATEGORIES_URL+s.id)
            .toClass<PicMoreCategoryBean>()
            .await()
    }
}